//
//  ViewController.m
//  Cards
//
//  Created by Min on 2023/11/25.
//

#define RandomColor [UIColor colorWithRed:(arc4random() % 256) / 255.0 \
                                     green:(arc4random() % 256) / 255.0 \
                                      blue:(arc4random() % 256) / 255.0 \
                                     alpha:1.0]


#import "ViewController.h"
#import "MKCards.h"
#import <Masonry.h>
#import "MKUIView+Extension.h"

@interface ViewController ()

@property(strong,nonatomic) NSMutableArray *views;
@property(strong,nonatomic) NSMutableArray *views_frame;
@property(strong,nonatomic) NSArray *below_views;
@property(strong,nonatomic) UIPanGestureRecognizer *panGesture;
@property(assign,nonatomic) CGPoint initialCenter;
@end

@implementation ViewController


CGFloat width = 300;
CGFloat height = 600;


- (void)viewDidLoad{
    [super viewDidLoad];
    
    
    
    self.view.backgroundColor = [UIColor orangeColor];
    
    NSArray *datas = @[@"1",@"2",@"3"];
    
    MKCards *cards = [[MKCards alloc]initWithFrame:CGRectMake(20, 100, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 200) datas:datas cardCount:3];
    
    [self.view addSubview:cards];
    
}

//- (void)viewDidLoad {
//    [super viewDidLoad];
//
//    self.views = [NSMutableArray array];
//    self.views_frame = [NSMutableArray array];
//    self.view.backgroundColor = [UIColor whiteColor];
//
//
//    for (int i = 0; i < 3; i++) {
//        UIView *view = [[UIView alloc]init];
//        view.backgroundColor = RandomColor;
//        view.layer.cornerRadius = 8;
//        view.layer.masksToBounds = NO;
//        [self.view addSubview:view];
//
//        // Add shadow properties
//        view.layer.shadowColor = [UIColor blackColor].CGColor;
//        view.layer.shadowOffset = CGSizeMake(0, 8);
//        view.layer.shadowOpacity = 0.2;
//        view.layer.shadowRadius = 3;
//        view.tag = i + 1;
//
//
//        // 使用frame设置位置和大小
//
//        view.frame = CGRectMake(self.view.center.x - width / 2 - i * 10,
//                                 self.view.center.y - i * 10 - height / 2,
//                                 width + i * 20,
//                                 height);
//        [self.views addObject:view];
//
//        if (i == 2){
//            // 添加拖动手势
//            self.panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
//            [view addGestureRecognizer:self.panGesture];
//        }
//
//
//    }
//}


// 拖动手势处理方法
- (void)handlePan:(UIPanGestureRecognizer *)gesture {
    UIView *view = gesture.view;
    CGPoint translation = [gesture translationInView:self.view];

    if (gesture.state == UIGestureRecognizerStateBegan) {
        // 记录拖动开始时的中心点
        self.initialCenter = view.center;
        [self.views_frame removeAllObjects];
        
        self.below_views = [self.views subarrayWithRange:NSMakeRange(0, self.views.count - 1)];
        
        for (int i = 0; i < self.below_views.count; i++) {
            UIView * view = self.below_views[i];
            [self.views_frame addObject:NSStringFromCGRect(view.frame)];
        }
    }
    
    
    if (translation.y <= 0){
        view.alpha = 1 - fabs(translation.y) / view.frame.size.height;
        
        
        //NSLog(@"%f",translation.y);
        // 根据手指的拖动距离更新视图的中心点
        view.center = CGPointMake(self.initialCenter.x, self.initialCenter.y + translation.y);
        
        [self.below_views enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            [self view:obj scale:1 - view.alpha arrIndex:idx];
        }];
    }
    
    

    
    
    if (gesture.state == UIGestureRecognizerStateEnded) {
        
        
        
        if (translation.y > -50){
            [UIView animateWithDuration:0.25 delay:0
                 usingSpringWithDamping:1 // 调整这个值来改变弹簧效果的强度
                  initialSpringVelocity:0.1  // 调整这个值来改变弹簧效果的速度
                                options:UIViewAnimationOptionCurveEaseInOut
                             animations:^{
                                 view.center = self.initialCenter;
                                 view.alpha = 1;
                             }
                             completion:nil];
            
            for (int i = 0; i < self.below_views.count; i++) {
                UIView *view = self.below_views[i];
                view.frame = CGRectFromString(self.views_frame[i]);
            }

        }else{
            [UIView animateWithDuration:0.25 animations:^{
                view.center = CGPointMake(self.initialCenter.x, self.initialCenter.y - view.frame.size.height);
                view.alpha = 0;
            } completion:^(BOOL finished) {
                
                if (finished){
                    [view removeFromSuperview];
                    [self.view insertSubview:view belowSubview:self.views.firstObject];
                    [self.views removeObject:view];
                    [self.views insertObject:view atIndex:0];
                    
                    
                    
                    
                    view.alpha = 1;
                    for (int i = 0; i < self.views.count; i++) {
                        UIView *view = self.views[i];
                        if (i == 0){
                            view.frame = CGRectMake(self.view.center.x - width / 2 - i * 10,
                                                    self.view.center.y - i * 10 - height / 2,
                                                    width + i * 20,
                                                    height);
                        }else{
                            [UIView animateWithDuration:0.25 animations:^{
                                view.frame = CGRectMake(self.view.center.x - width / 2 - i * 10,
                                                        self.view.center.y - i * 10 - height / 2,
                                                        width + i * 20,
                                                        height);
                            }];
                        }
                        
                        if (i == self.views.count - 1){
                            [view addGestureRecognizer:self.panGesture];
                        }
                    }
                }
                
            }];
        }
        
    }
}

- (void)view:(UIView *)view scale:(CGFloat)scale arrIndex:(NSInteger)index{
    NSString *frame_str = self.views_frame[index];
    CGRect origin_frame = CGRectFromString(frame_str);
    view.frame = CGRectMake(origin_frame.origin.x - scale * 10,
                            origin_frame.origin.y - scale * 10,
                            origin_frame.size.width + scale * 20,
                            origin_frame.size.height);

}


@end
